package com.example.aspect;

import com.example.annotations.UserAble;
import com.example.util.SpelParser;
import com.example.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class UserAbleParser {

    @Autowired
    CachePutUtils cachePutUtils;

    public static final String RETURN_PARAM="proceed";

    @Around("@annotation(userAble)")
    public Object parse(ProceedingJoinPoint joinPoint, UserAble userAble) throws Throwable {
        Object[] args = joinPoint.getArgs();
        Object arg = args[0];
        String key = userAble.key();
        String value = userAble.value();
        if (key.contains("#")){
            key = SpelParser.getKey(key, joinPoint);
            key="aaa";
        }
        if (value.contains("#")){
            value = SpelParser.getKey(value, joinPoint);
        }
        log.info("key:{},value:{}",key,value);
        Object proceed = joinPoint.proceed();
        cachePutUtils.cache((UserVO) proceed);
        String proceedKey = userAble.proceed();
        String proceedKeyParse = SpelParser.getKey(proceedKey, RETURN_PARAM, proceed,String.class);
        log.info("proceedKeyParse:{}",proceedKeyParse);
//        if(proceedKeyParse!=null && proceedKeyParse){
//            log.info("proceedKeyParseResult:{}",proceedKeyParse);
//        }
        return proceed;
    }

}
